// pages/add/add.js
var app = getApp()
var i = 0;
var formDatas = null;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    imgt:[],
    imgst:[],
    array:['美食','游乐','旅行','风景'],
    objectArray:[
      {
        id:0,
        name:'美食'
      },
      {
        id:1,
        name:'游乐'
      },
      {
        id:2,
        name:'旅行'
      },
      {
        id:3,
        name:'风景'
      }
    ],
    index:0,
    address:"请选择你的位置",
    upload_picture_list: [],
    imgServer: [],
    formdatas:null
  },
  //获取位置
  chooseMapViewTap: function () {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
            address: res.address
        })
      },
    })
  },

  // 上传图片
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 5) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function () {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    wx.chooseImage({
      // count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(res);
        var imgs = that.data.imgs;
        // console.log(tempFilePaths + '----');
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 4) {
            wx.showToast({
              title: '只能选四张',
            })
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths[i]);
          }
        }
        
        that.setData({
          imgs: imgs
        })

      }
    });

  },
  // 删除图片
  deleteImg: function (e) {
    console.log(e)
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },


  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  

  clearFont() {
    this.setData({
      placeholder: ''
    })
  },

  cancele(e) {
    if (e.detail.value == null){
      placeholder: '游记类型'
    }
  },



  //表单上传
  formSubmit:function(e) {
    var that = this;
    formDatas = e.detail.value;
    console.log(formDatas)
    var imgs = that.data.imgs;
    var imgt = that.data.imgt;
    var ututype = '';
    if (formDatas.类型 == 0 ){
      ututype = '美食';
    } else if (formDatas.类型 == 0){
      ututype = '游乐';
    } else if (formDatas.类型 == 0) {
      ututype = '旅行';
    } else{
      ututype = '风景';
    }
   

    if (app.globalData.alltoken == null){
      wx: wx.showToast({
        title: '请先登录',
        icon: 'none',
      })
    } else { 

    if (e.detail.value.游记内容 == ''  || e.detail.value.地址 == '请选择你的位置' || that.data.imgs.length == 0) {
      wx.showToast({
        title: '请填写完整',
        icon: 'none'
      })
    } else {
      for (i = 0; i < that.data.imgs.length; i++) {
        wx.uploadFile({
          url: app.globalData.url + '/v1/UploadFile/uploadFile', 
          filePath: imgs[i],
          header:{
        
            "token": app.globalData.alltoken
          },
          name: 'StrategyImg',
          success(res) {
            
            var imgst = that.data.imgst
            var dataimg = JSON.parse(res.data)
            imgt.push(dataimg.data.sImgs)

            for (var i = 0; i < imgt.length; i++) {
              imgst[i]=imgt[i];
            }
            // console.log("111" + imgst)
            // console.log(that.data.imgst)
          },
        })
      }
      
      //shangchuanhoutai
        wx: wx.request({
          url: app.globalData.url + '/v1/Strategy/saveStrategy',
          data: {
            stitle: "null",
            scost: 0,
            stype: ututype ,
            sdescribe: formDatas.游记内容,
            slocahost: that.data.address,
            simg: that.data.imgst
          },
          header: {
            "Content-Type": "application/json",
            "token": app.globalData.alltoken
          },
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            console.log(res)
            if (res.statusCode == 200) {
              wx: wx.showToast({
                title: '发布成功',
                icon: 'true'
              })
              that.setData({
                imgs: [],
                index: 0,
                address: "请选择你的位置",
                formdatas: null
              })
            }
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      

    }
  }
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})